﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BizCollegeWebApp.Models;
using System.Web.Security;

namespace BizCollegeWebApp.Controllers
{
    public class UserController : Controller
    {
        BizCollegeDBDataContext db = new BizCollegeDBDataContext();


        //
        // GET: /User/

        [HttpGet]
        public ActionResult Register()
        {
            return View(new User());
        }

        [HttpGet]
        public ActionResult WelcomeRegister()
        {
            return View("WelcomeRegister");
        }

        [HttpPost]
        public ActionResult Register(User usern)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    BizCollegeDBDataContext db = new BizCollegeDBDataContext();
                    BizCollegeWebApp.Models.User user = this.db.Users.FirstOrDefault(u => u.Username == usern.Username);
                    if (user == null)
                    {
                        this.db.Users.InsertOnSubmit(usern);
                        this.db.SubmitChanges();
                    }
                    else
                    {
                        ModelState.AddModelError("Username", "Username already exists");
                        return View(usern);
                    }

                    // return RedirectToAction("Index");
                    return RedirectToAction("WelcomeRegister");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ex", ex.Message);
            }
            return View(usern);
        }


        //
        // GET: /User/

        public ActionResult Index()
        {
            return View(this.db.Users.ToList());

        }

        // GET: /User/

        [HttpGet]
        public ActionResult Edit(int id)
        {
            BizCollegeWebApp.Models.User user = this.db.Users.FirstOrDefault(u => u.ID == id);
            if (user != null)
            {
                return View(user);
            }
            return RedirectToAction("Index");
        }


        [HttpPost]
        public ActionResult Edit(int id, User vUser)
        {
            BizCollegeWebApp.Models.User user = this.db.Users.FirstOrDefault(u => u.ID == id);
            if (user == null)
            {
                return RedirectToAction("Index");
            }
            if (TryUpdateModel(user))
            {
                this.db.SubmitChanges();
                if (user.ID == Current.User.ID)
                {
                    Current.User = user;
                }
                return RedirectToAction("Profile", "Account", user);
            }
            return View(vUser);
        }

        //
        // GET: /User/

        public ActionResult Delete(int id)
        {
            BizCollegeWebApp.Models.User user = this.db.Users.FirstOrDefault(u => u.ID == id);
            if (user != null)
            {
                this.db.Users.DeleteOnSubmit(user);
                this.db.SubmitChanges();
            }
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id)
        {
            return View();
        }
    }
}
